home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Mac OS SDK / Dev.CD Jan 98 SDK1.toast / Development Kits (Disc 1) / PC Card SDKs / PC Card Manager 3.0 / TupleDumper / TupleDumper.c next >
Encoding:
C/C++ Source or Header  |  1997-06-05  |  7.8 KB  |  316 lines  |  [TEXT/CWIE]

  1. //----------------------------------------------------------------------
  2. //
  3. //    Module:
  4. //        TupleDumper.c
  5. //
  6. //    Purpose:
  7. //        Use the PCCardFPI interface to dump tuples of any inserted cards
  8. //
  9. //    Author:
  10. //        Dave Tarabar
  11. //
  12. //    Copyright:    © 1996 SystemSoft Corporation, all rights reserved.
  13. //----------------------------------------------------------------------
  14.  
  15. #include <stdio.h>
  16. #include <DriverSupport.h>
  17. #include <Errors.h>
  18. #include <PCCard.h>
  19. #include <PCCardTuples.h>
  20. #include <TextUtils.h>
  21.  
  22. //----------
  23. void ByteToChars(UInt8 inByte, char* leftChar, char* rightChar);
  24. void FormatTheTuple(UInt8 tupleCode, UInt16 len, char* tupleBody, char* buffer);
  25. OSErr PostNotification(Str255 msg, OSErr errCode);
  26. OSStatus DumpTheTuples(const RegEntryRef * cardRef);
  27.  
  28.  
  29. //--------------------------------------------------------------------------------
  30. void main()
  31. {
  32.     OSStatus        err;
  33.     ItemCount         socketCount;
  34.     UInt32            complianceLevel, version;
  35.     PCCardSocket    i;
  36.     RegEntryRef        cardRef;
  37.     
  38.     // how many socket are there
  39.     err = PCCardGetCardServicesInfo(&socketCount, &complianceLevel, &version);
  40.     if (err != noErr)
  41.         PostNotification("\pPCCardGetCardServicesInfo failed", err);
  42.  
  43.     // loop through all of the sockets, looking for cards
  44.     for (i = 0; i < socketCount; i++)
  45.     {
  46.         err = PCCardGetCardRef(i, &cardRef);
  47.         if (err == noErr)
  48.         {
  49.             // found a card, dump it's tuples
  50.             printf("Card in Socket %d\n\n", i);
  51.             DumpTheTuples(&cardRef);
  52.             printf("\n");
  53.         }
  54.     }
  55. }
  56.  
  57.  
  58. //--------------------------------------------------------------------------------
  59. OSStatus DumpTheTuples(const RegEntryRef * cardRef)
  60. {
  61.     OSStatus                err;
  62.     PCCardTupleIterator     tupleIterator;
  63.     Byte                     buffer[MAX_TUPLE_SIZE];
  64.     char                    outBuffer[1500];                // big enough ??
  65.     UInt32                    bufferSize, tupleSize;
  66.     PCCardTupleKind            foundTuple;
  67.  
  68.     // allocate a tuple iterator
  69.     tupleIterator = PCCardNewTupleIterator();
  70.     if (tupleIterator == NULL)
  71.         return memFullErr;
  72.  
  73.     // get the first tuple
  74.     bufferSize = MAX_TUPLE_SIZE;
  75.     err = PCCardGetFirstTuple(cardRef, 0xFF, tupleIterator, buffer, &bufferSize, &foundTuple, &tupleSize);
  76.  
  77.     while (err == noErr)
  78.     {
  79.         // output the current tuple
  80.         FormatTheTuple(foundTuple, tupleSize, (char *) buffer, outBuffer);
  81.         printf (outBuffer);
  82.  
  83.         // get the next tuple
  84.         bufferSize = MAX_TUPLE_SIZE;
  85.         err = PCCardGetNextTuple(cardRef, 0xFF, tupleIterator, buffer, &bufferSize, &foundTuple, &tupleSize);
  86.     }
  87.  
  88.     // that's all folks
  89.     (void) PCCardDisposeTupleIterator(tupleIterator);
  90.     return noErr;
  91. }
  92.  
  93.  
  94. //--------------------------------------------------------------------------------
  95. // do a hex dump of a tuple with the 1 byte code, a space & then a dump of the data
  96. void FormatTheTuple(UInt8 tupleCode, UInt16 len, char* tupleBody, char* buffer)
  97. {
  98.     short              count = 0, i;
  99.     char            foo, bar;
  100.     
  101.     ByteToChars(tupleCode, &foo, &bar);
  102.     buffer[count++] = foo;
  103.     buffer[count++] = bar;
  104.     buffer[count++] = ' ';
  105.     buffer[count++] = 0;
  106.  
  107.     switch (tupleCode)
  108.     {
  109.         case    CISTPL_NULL:
  110.             CStrCat(buffer,     "CISTPL_NULL ");
  111.             break;
  112.         case    CISTPL_DEVICE:
  113.             CStrCat(buffer,     "CISTPL_DEVICE ");
  114.             break;
  115.         case    CISTPL_LONGLINK_CB:
  116.             CStrCat(buffer,     "CISTPL_LONGLINK_CB ");
  117.             break;
  118.         case    CISTPL_INDIRECT:
  119.             CStrCat(buffer,     "CISTPL_INDIRECT ");
  120.             break;
  121.         case    CISTPL_LONGLINK_MFC:
  122.             CStrCat(buffer,     "CISTPL_LONGLINK_MFC ");
  123.             break;
  124.         case    CISTPL_CHECKSUM:
  125.             CStrCat(buffer,     "CISTPL_CHECKSUM ");
  126.             break;
  127.         case    CISTPL_LONGLINK_A:
  128.             CStrCat(buffer,     "CISTPL_LONGLINK_A ");
  129.             break;
  130.         case    CISTPL_LONGLINK_C:
  131.             CStrCat(buffer,     "CISTPL_LONGLINK_C ");
  132.             break;
  133.         case    CISTPL_LINKTARGET:
  134.             CStrCat(buffer,     "CISTPL_LINKTARGET ");
  135.             break;
  136.         case    CISTPL_NO_LINK:
  137.             CStrCat(buffer,     "CISTPL_NO_LINK ");
  138.             break;
  139.         case    CISTPL_VERS_1:
  140.             CStrCat(buffer,     "CISTPL_VERS_1 ");
  141.             break;
  142.         case    CISTPL_ALTSTR:
  143.             CStrCat(buffer,     "CISTPL_ALTSTR ");
  144.             break;
  145.         case    CISTPL_DEVICE_A:
  146.             CStrCat(buffer,     "CISTPL_DEVICE_A ");
  147.             break;
  148.         case    CISTPL_JEDEC_C:
  149.             CStrCat(buffer,     "CISTPL_JEDEC_C ");
  150.             break;
  151.         case    CISTPL_JEDEC_A:
  152.             CStrCat(buffer,     "CISTPL_JEDEC_A ");
  153.             break;
  154.         case    CISTPL_CONFIG:
  155.             CStrCat(buffer,     "CISTPL_CONFIG ");
  156.             break;
  157.         case    CISTPL_CFTABLE_ENTRY:
  158.             CStrCat(buffer,     "CISTPL_CFTABLE_ENTRY ");
  159.             break;
  160.         case    CISTPL_DEVICE_OC:
  161.             CStrCat(buffer,     "CISTPL_DEVICE_OC ");
  162.             break;
  163.         case    CISTPL_DEVICE_OA:
  164.             CStrCat(buffer,     "CISTPL_DEVICE_OA ");
  165.             break;
  166.         case    CISTPL_DEVICE_GEO:
  167.             CStrCat(buffer,     "CISTPL_DEVICE_GEO ");
  168.             break;
  169.         case    CISTPL_DEVICE_GEO_A:
  170.             CStrCat(buffer,     "CISTPL_DEVICE_GEO_A ");
  171.             break;
  172.         case    CISTPL_MANFID:
  173.             CStrCat(buffer,     "CISTPL_MANFID ");
  174.             break;
  175.         case    CISTPL_FUNCID:
  176.             CStrCat(buffer,     "CISTPL_FUNCID ");
  177.             break;
  178.         case    CISTPL_FUNCE:
  179.             CStrCat(buffer,     "CISTPL_FUNCE ");
  180.             break;
  181.         case    CISTPL_SWIL:
  182.             CStrCat(buffer,     "CISTPL_SWIL ");
  183.             break;
  184.         case    CISTPL_VERS_2:
  185.             CStrCat(buffer,     "CISTPL_VERS_2 ");
  186.             break;
  187.         case    CISTPL_FORMAT:
  188.             CStrCat(buffer,     "CISTPL_FORMAT ");
  189.             break;
  190.         case    CISTPL_GEOMETRY:
  191.             CStrCat(buffer,     "CISTPL_GEOMETRY ");
  192.             break;
  193.         case    CISTPL_BYTEORDER:
  194.             CStrCat(buffer,     "CISTPL_BYTEORDER ");
  195.             break;
  196.         case    CISTPL_DATE:
  197.             CStrCat(buffer,     "CISTPL_DATE ");
  198.             break;
  199.         case    CISTPL_BATTERY:
  200.             CStrCat(buffer,     "CISTPL_BATTERY ");
  201.             break;
  202.         case    CISTPL_ORG:
  203.             CStrCat(buffer,     "CISTPL_ORG ");
  204.             break;
  205.         case    CISTPL_VENDOR:
  206.             CStrCat(buffer,     "CISTPL_VENDOR ");
  207.             break;
  208.         case    CISTPL_END:
  209.             CStrCat(buffer,     "CISTPL_END ");
  210.             break;
  211.             
  212.         default:
  213.             CStrCat(buffer,     "???? ");
  214.             break;
  215.         
  216.     }
  217.     
  218.     count = CStrLen(buffer);
  219.     for (i=0; i<len; i++)
  220.     {
  221.         ByteToChars(tupleBody[i], &foo, &bar);
  222.         buffer[count++] = foo;
  223.         buffer[count++] = bar;
  224.     }
  225.     buffer[count++] = 0;
  226.         
  227.     // print out the ascii of manufacturer and card name for CISTPL_VERS_1
  228.     if (tupleCode == CISTPL_VERS_1)
  229.     { 
  230.         CStrCat(buffer,"\n   manufacturer = ");
  231.         CStrCat(buffer, (const char *) &tupleBody[2]);
  232.         CStrCat(buffer,"\n   card name = ");
  233.         for (i=2; (tupleBody[i] != 0) && (i < CStrLen(tupleBody)); i++) {}
  234.         i++;
  235.         CStrCat(buffer, (const char *) &tupleBody[i]);
  236.     }
  237.  
  238.     // print out an ascii name of device type for CISTPL_FUNCID
  239.     if (tupleCode == CISTPL_FUNCID)
  240.     { 
  241.         CStrCat(buffer,"\n   type = ");
  242.         switch (tupleBody[0])
  243.         {
  244.             case TPLFID_MultiFunction:
  245.                 CStrCat(buffer, "multi-function card");
  246.                 break;
  247.             case TPLFID_Memory:
  248.                 CStrCat(buffer, "memory card");
  249.                 break;
  250.             case TPLFID_SerialPort:
  251.                 CStrCat(buffer, "serial port or modem");
  252.                 break;
  253.             case TPLFID_ParallelPort:
  254.                 CStrCat(buffer, "parallel port");
  255.                 break;
  256.             case TPLFID_FixedDisk:
  257.                 CStrCat(buffer, "fixed disk");
  258.                 break;
  259.             case TPLFID_VideoAdapter:
  260.                 CStrCat(buffer, "video adapter");
  261.                 break;
  262.             case TPLFID_NetworkLANAdapter:
  263.                 CStrCat(buffer, "network adapter");
  264.                 break;
  265.             case TPLFID_AIMS:
  266.                 CStrCat(buffer, "AIMS");
  267.                 break;
  268.             case TPLFID_SCSI:
  269.                 CStrCat(buffer, "SCSI");
  270.                 break;
  271.             default:
  272.                 CStrCat(buffer, "????");
  273.                 break;
  274.         }
  275.     }
  276.     CStrCat(buffer, "\n");
  277.     CStrCat(buffer, "\0");
  278. }
  279.  
  280.  
  281. //--------------------------------------------------------------------------------
  282. // convert the input byte to the two characters of it's hexadecimal representation
  283. void ByteToChars(UInt8 inByte, char* leftChar, char* rightChar)
  284. {
  285.     static char        trans[16] = {    '0', '1', '2', '3', '4', '5', '6', '7',
  286.                                     '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'    };
  287.     short    R = inByte & 0x0F;
  288.     short    L = (inByte & 0xF0) >> 4;
  289.     
  290.     *leftChar  = trans[L];
  291.     *rightChar = trans[R];
  292. }
  293.  
  294.  
  295. //--------------------------------------------------------------------------------
  296. // Notify some one that an error has occurred.
  297. OSErr PostNotification(Str255 msg, OSErr errCode)
  298. {    
  299.     Str255        gNotifyAlertText;
  300.  
  301.     // build an appropriate text string for the alert
  302.     PStrCopy(gNotifyAlertText, msg);
  303.     if (errCode != noErr)
  304.     {
  305.         Str31        errText;
  306.  
  307.         NumToString(errCode, errText);
  308.         PStrCat(gNotifyAlertText, "\p, code = ");
  309.         PStrCat(gNotifyAlertText, errText);
  310.     }
  311.     DebugStr(gNotifyAlertText);
  312.  
  313.     return noErr;
  314. }
  315.  
  316.